package com.hitqz.robot.biz.flow.component.agv;

import com.alibaba.fastjson.JSONObject;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 *
 * @author xupkun
 * @date 2025/2/25
 */

@Slf4j
@LiteflowComponent(value = "EnterLTK", name = "[轮对/轮轴]进入立体库请求")
@AllArgsConstructor
public class EnterLTK extends NodeComponent {

    private final RestTemplate restTemplate;

    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        //FlowContextDto dto =  this.getRequestData();
        //FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
        //        .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        //String auto = ticketsDto.getModalData().get("auto")== null ? "0":
        //        String.valueOf(ticketsDto.getModalData().get("auto"));
        //if (auto.equals("0")) {
        //    log.info("半自动任务:");
        //    return ;
        //}

        while (true) {
            try {
                LiteFlowUtil.checkInterruptFlag();
                String uri = "http://192.10.13.20:9999/lzms/ldltkInRequest";
                log.info("确认可以进入立体库的请求是:" + uri);
                String result = restTemplate.postForObject(
                        uri, "", String.class);
                log.info("确认可以立体库的请求结果是:" + result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.containsKey("data") && jsonObject.getInteger("data") == 1) {
                    log.info("立体库可以取走:");
                    break;
                }
                TimeUnit.SECONDS.sleep(3);
            } catch (Exception e) {
                log.info("请求进入立体库失败：" + e.getMessage());
            }

        }

    }


}
